package com.ruoyi.comment.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.comment.domain.PostPlayComment;
import com.ruoyi.comment.service.IPostPlayCommentService;
import com.ruoyi.comment.vo.PostPlayCommentListVo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.play.domain.PostPlay;
import com.ruoyi.play.service.IPostPlayService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论Controller
 *
 * @author ruoyi
 * @date 2025-05-21
 */
@RestController
@RequestMapping("/comment")
public class PostPlayCommentController extends BaseController {
    @Autowired
    private IPostPlayCommentService postPlayCommentService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IPostPlayService postPlayService;

    /**
     * 查询评论列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PostPlayComment postPlayComment) {
        List<PostPlayCommentListVo> res = new ArrayList<>();
        PostPlay postPlay = postPlayService.selectPostPlayById(postPlayComment.getPlayId());
        if (postPlay == null){
            throw new ServiceException("组队信息不存在!!");
        }
        LoginUser loginUser = getLoginUser();
        Long userId = loginUser.getUser().getUserId();
        // 如果是自己可见,添加用户id筛选
        if (Integer.valueOf(1).equals(postPlayComment.getType())) {
            postPlayComment.setUserId(userId);
        }
        // 如果是组队发起者,则不添加用户id筛选
        if (userId.equals(postPlay.getOwnerId())){
            postPlayComment.setUserId(null);
        }
        List<PostPlayComment> list = postPlayCommentService.selectPostPlayCommentList(postPlayComment);
        if (CollectionUtil.isNotEmpty(list)) {
            List<Long> userIdList = list.stream().map(PostPlayComment::getUserId).collect(Collectors.toList());
            List<SysUser> sysUserList = sysUserService.selectUserByIds(userIdList);
            Map<Long, SysUser> userMap = sysUserList.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
            list.forEach(comment -> {
                PostPlayCommentListVo commentListVo = new PostPlayCommentListVo();
                SysUser user = userMap.get(comment.getUserId());
                if (user != null) {
                    commentListVo.setUserId(user.getUserId());
                    commentListVo.setNickName(user.getNickName());
                    commentListVo.setAvatar(user.getAvatar());
                    boolean equals = user.getUserId().equals(userId);
                    commentListVo.setMe(equals);
                }
                commentListVo.setPostPlayComment(comment);
                res.add(commentListVo);
            });
        }
        return getDataTable(res);
    }

    /**
     * 导出评论列表
     */
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostPlayComment postPlayComment) {
        List<PostPlayComment> list = postPlayCommentService.selectPostPlayCommentList(postPlayComment);
        ExcelUtil<PostPlayComment> util = new ExcelUtil<PostPlayComment>(PostPlayComment.class);
        util.exportExcel(response, list, "评论数据");
    }

    /**
     * 获取评论详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(postPlayCommentService.selectPostPlayCommentById(id));
    }

    /**
     * 新增评论
     */
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostPlayComment postPlayComment) {
        return toAjax(postPlayCommentService.insertPostPlayComment(postPlayComment));
    }

    /**
     * 修改评论
     */
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostPlayComment postPlayComment) {
        return toAjax(postPlayCommentService.updatePostPlayComment(postPlayComment));
    }

    /**
     * 删除评论
     */
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(postPlayCommentService.deletePostPlayCommentByIds(ids));
    }
}
